/**
 * Created by jiangzhiyong on 15/10/14.
 */

var Manage = {
    init : function() {
        Manage.get_data();
        $("#data_table tbody").find("a[name='update_user']").click(Manage.user_show_update);
        $("a[name='add_user']").click(Manage.user_show_add);
        $("#data_table tbody").find("a[name='delete_user']").click(function() {
        		var td = $(this).parent();
    			var user_id = $(td).attr("user_id");
    			if (confirm("确定要删除该品牌?")) {
    				//编辑
        			ajax("/users/remove", {"user_id": user_id}, success);
    			}
        });
        $("#do_update_user").click(function(){
        		var user_id = $("#update_user").find("input[name='user_id']").val();
        		var user_name = $("#update_user").find("input[name='user_name']").val();
        		var user_wiki = $("#update_user").find("textarea[name='user_wiki']").val();
        		if (user_name == null || user_name == "") {
        			alert("科目名称不能为空");
        			return false;
        		}
        		if(user_id == "" || user_id == 0) {
        			//新增
        			ajax("/users/create", {"name" : user_name, "wiki" : user_wiki}, success);
        		} else {
        			
        		}
        });
        $(".pagination li a").click(function() {
    			var page = $(this).attr("page");
    			var size = 10;
    			Manage.get_data(page,size);
    			Common.init_pagination(page);
    		});
    },
    get_data : function(page, size) {
    		if (page == undefined) {
    			page = 0;
    		}
    		if (size == undefined) {
    			size = 10;
    		}
        var data = {
        		"page" : page,
        		"size" : size
        };
        ajax("/users/list", data, callback);
    },
    user_show_add : function() {
    		$("#update_user").find("input[name='user_id']").val(0);
    		$("#update_user").find("input[name='user_name']").val("");
    		$("#update_user").find("textarea[name='user_wiki']").val("");
    		$("#update_user").show();
    },
    user_show_update : function() {
    		var td = $(this).parent();
    		var user_id = $(td).attr("user_id");
    		$("#update_user").find("input[name='user_id']").val(user_id);
    		var user_name = $(td).prev().prev().text();
    		$("#update_user").find("input[name='user_name']").val(user_name);
    		var user_wiki = $(td).prev().text();
    		$("#update_user").find("textarea[name='user_wiki']").val(user_wiki);
    		$("#update_user").show();
    },
    formatDate(ns) {
    		var now = new Date(ns * 1000);
       	var date = {
        		"M+": now.getMonth() + 1,
            "d+": now.getDate(),
            "h+": now.getHours(),
            "m+": now.getMinutes(),
            "s+": now.getSeconds(),
            "q+": Math.floor((now.getMonth() + 3) / 3),
            "S+": now.getMilliseconds()
       	};
       	var format = "yyyy-MM-dd hh:mm:ss";
       	if (/(y+)/i.test(format)) {
       		format = format.replace(RegExp.$1, (now.getFullYear() + '').substr(4 - RegExp.$1.length));
       	}
       	for (var k in date) {
      		if (new RegExp("(" + k + ")").test(format)) {
      			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        		}
       	}
       	return format;
	},
    init_table : function(result) {
    		if (result != null && result.data != null && result.data.users.length >= 0) {
    			var tbody = $("#data_table tbody");
    			tbody.empty();
    			var users = result.data.users;
    			var trs = "";
    			for (var i=0; i < users.length; i++) {
    				var userOne = users[i];
    				/** {
	                "birthday": 0,
	                "created_at": 1.407891234e+09,
	                "gender": 1,
	                "id": 1,
	                "mobile": "13800000000",
	                "name": "admin",
	                "nick": "admin",
	                "slogan": "我是管理员",
	                "status": 0,
	                "type": 1,
	                "wiki": "我是管理员"
	            	 }
    				 * */
    				trs += ("<tr>");
    				var id = ("<td>" + userOne.id +"</td>");
    				var name = ("<td>" + userOne.name +"</td>");
    				var nick = ("<td>" + (userOne.nick == undefined ? "" : userOne.nick) +"</td>");
    				var mobile = ("<td>" + userOne.mobile +"</td>");
    				var created_at = ("<td>" + Manage.formatDate(userOne.created_at) +"</td>");
    				var birthday = ("<td>" + Manage.formatDate(userOne.birthday) +"</td>");
    				var genderDesc = "未知";
    				if (userOne.gender == 0) {
    					genderDesc = "未知";
    				} else if (userOne.gender == 1) {
    					genderDesc = "女性";
    				} else if (userOne.gender == 2) {
    					genderDesc = "男性";
    				}
    				var gender = ("<td>" + genderDesc +"</td>");
    				var slogan = ("<td>" + (userOne.slogan == undefined ? "" : userOne.slogan) +"</td>");
//  				var status = ("<td>" + userOne.status +"</td>");
    				var typeDesc = "未知";
    				if (userOne.type == 0) {
    					typeDesc = "学生";
    				} else if (userOne.type == 1) {
    					typeDesc = "管理员";
    				} else if (userOne.type == 2) {
    					typeDesc = "教练";
    				}
    				var type = ("<td>" + typeDesc +"</td>");
    				var wiki = ("<td>" + (userOne.wiki == undefined ? "" : userOne.wiki) +"</td>");
    				trs += ( id + type + mobile + name + nick + gender + slogan + wiki + birthday + created_at);
    				trs += ("</tr>");
    			}
    			if (users.length == 0) {
				trs += ("<tr>");
				trs += ("<td colspan='11'>未查询到任何结果</td>");
				trs += ("</tr>");
			}
    			tbody.append(trs);
    		}
    }
};

function callback(result) {
	Manage.init_table(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function(){
    Manage.init();
})
